<template>
  <!--证书-->
  <view>
    <myHead
      :title="title"
      :right="true"
      text_right="保存"
      @clickRight="clickRight"
    ></myHead>
    <view class="conten">
      <view class="li-box fl-lr-c">
        <view class="name">证书名称</view>
        <view>
          <input
            placeholder-class="input_pal"
            type="text"
            v-model="name"
            placeholder="请输入"
          />
        </view>
      </view>

      <u-datetime-picker
        :show="show"
        v-if="show"
        mode="date"
        v-model="value"
        closeOnClickOverlay
        @close="show = false"
        @confirm="getDate"
        :minDate="minDate"
        :maxDate="maxDate"
        @cancel="show = false"
      ></u-datetime-picker>

      <view class="li-box fl-lr-c" @click="getTimeD">
        <view class="name">获取时间</view>
        <view class="fl-t">
          <view class="content">
            <text v-if="getTime">{{ getTime | formatDateYM }}</text>
            <text v-else>请选择</text>
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
    </view>
    <bottomBut @bottomBut="bottomBut" v-if="type == 2"></bottomBut>
  </view>
</template>

<script>
import myHead from "@/pages/component/baseHead.vue";
import bottomBut from "@/pages/component/bottomBut.vue";
import utils from "@/common/tui-utils/tui-utils.js";
import { atPresentTime, atPresentTimeX, TimeString } from "@/utils/Time";
export default {
  data() {
    return {
      show: false,
      value1: "",
      year: 2022, //当前的年份
      type: 0, ////1add  2编辑
      title: "获得证书",
      name: "", //证书名字
      getTime: "",
      maxDate: "", //最大时间
      minDate: "",
      value: "",
      // 当前id
      Id: 0,
      // 父id
      fuid: 0,
    };
  },
  //子组件
  components: {
    myHead,
    bottomBut,
  },
  onLoad(o) {
    //#region 证书时间限制
    this.maxDate = atPresentTime();
    this.minDate = atPresentTimeX(60);
    //#endregion
    this.fuid = o.fuid;
    this.type = o.type;
    if (this.type == 2) {
      let data = JSON.parse(o.data);
      this.Id = data.Id;
      this.name = data.Name;
      this.getTime = data.Date.split(" ")[0];
    }
  },
  // 计算方法
  methods: {
    getTimeD() {
      this.value = this.getTime ? this.getTime : TimeString(this.maxDate);
      this.show = true;
    },
    /**
     * 获取选择的日期
     * @description 将时间戳结果转为年月日字符串
     * @param {Object} e - 结果
     * @return void
     */
    getDate(e) {
      // console.log(e)
      const timeFormat = uni.$u.timeFormat;
      console.log(timeFormat(e.value, "yyyy-mm-dd"));
      console.log(timeFormat(e.value, "yyyy-mm-dd hh:MM"));
      console.log(timeFormat(e.value, "yyyy-mm"));

      let ds = timeFormat(e.value, "yyyy-mm-dd");

      console.log("ds");
      console.log(ds);
      this.getTime = ds;
      this.show = false;
    },

    clickRight() {
      if (!utils.trim(this.name).length) {
        this.$util.toast("请输入证书名字");
        return;
      }
      if (!utils.trim(this.getTime).length) {
        this.$util.toast("请选择时间");
        return;
      }
      if (this.type == 2) {
        //编辑修改
        let data = {
          Id: this.Id,
          ResumeId: this.fuid,
          Name: this.name,
          Date: this.getTime,
          Images: "",
          SortCode: "",
        };
        this.$http.huirc.resumecertificateup(data).then((res) => {
          if (res.Success) {
            this.$util.toast("保存成功");
            setTimeout(() => {
              uni.navigateBack();
            }, 1000);
          }
        });
      } else {
        let data = {
          ResumeId: this.fuid,
          Name: this.name,
          Date: this.getTime,
          Images: "",
          SortCode: "",
        };
        this.$http.huirc.resumecertificatecr(data).then((res) => {
          if (res.Success) {
            this.$util.toast("保存成功");
            setTimeout(() => {
              uni.navigateBack();
            }, 1000);
          }
        });
      }
    },
    bottomBut() {
      let that = this;
      console.log("删除");
      uni.showModal({
        title: "删除提示",
        content: "确认要删除这条数据",
        success: function (res) {
          if (res.confirm) {
            let data = {
              Id: that.Id,
            };

            that.$http.huirc.resumecertificatedel(data).then((res) => {
              if (res.Success) {
                that.$util.toast("删除成功");
                setTimeout(() => {
                  uni.navigateBack();
                }, 1600);
              }
            });
          }
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.li-box {
  background-color: #fff;
  .name {
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: bold;
    color: #000000;
    flex-shrink: 0;
  }

  .logo {
    width: 106rpx;
    height: 106rpx;
    border-radius: 50%;
  }

  .input_pal,
  input,
  .content {
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: #999999;
    text-align: end;
  }

  .radio-box {
    margin-left: 50rpx;

    .radio-text {
      font-size: 30rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: #999999;
      padding-left: 20rpx;
    }
  }

  .tui-textarea {
    flex: 1;
  }
}
</style>
